namespace AutoTest.Core.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Practices.ServiceLocation;
    using Ninject.Core;

    /// <summary>
    /// Implemented super crappy as i expect Ninject author coming out w/ good version soon
    /// </summary>
    public class QuickNinjectServiceLocator : IServiceLocator
    {
        readonly IKernel _kernel;

        public QuickNinjectServiceLocator(IKernel kernel)
        {
            _kernel = kernel;
        }

        public object GetService(Type serviceType)
        {
            throw new NotImplementedException();
        }

        public object GetInstance(Type serviceType)
        {
            throw new NotImplementedException();
        }

        public object GetInstance(Type serviceType, string key)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<object> GetAllInstances(Type serviceType)
        {
            var bindings = _kernel.Components.BindingRegistry.GetBindings(serviceType).FilterNull();
            foreach(var binding in bindings)
            {
                yield return _kernel.Get(binding.Provider.Prototype);
            }
        }

        public TService GetInstance<TService>()
        {
            return _kernel.Get<TService>();
        }

        public TService GetInstance<TService>(string key)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<TService> GetAllInstances<TService>()
        {
            return GetAllInstances(typeof(TService)).Cast<TService>();
        }
    }
}